package cn.demomaster.quickui.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import cn.demomaster.quicklogger.QdLog;

/**
 * Created by Squirrel桓 on 2018/12/12.
 */
public abstract class NetWorkBroadcastReceiver extends BroadcastReceiver implements NetStateChangedListener{
    private static final String TAG = NetWorkBroadcastReceiver.class.getSimpleName();
    public NetWorkBroadcastReceiver(){
    }

    private int netState = -400;//0不可用，1可用，-400未初始化
    public int getNetState() {//0不可用，1可用，-400未初始化
        return netState;
    }

    private boolean isReconnect;
    @Override
    public void onReceive(Context context, Intent intent) {
        /*if (netStateChangedListener != null) {
            netStateChangedListener.onReceive(context, intent);
        }*/
        String action = intent.getAction();
        //QdLog.e("action=" + action);
//        if (!TextUtils.isEmpty(action)) {
//            String des = NetWatchdog.getActionDescription(action);
//            switch (action) {
//                case WifiManager.WIFI_STATE_CHANGED_ACTION:
//                    int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
//                    String wifiDesc = NetWatchdog.getWifiStateDes(wifiState);
//                    //QdLog.println("网络状态变化,hash:" + hashCode() + ",action=" + action + "," + wifiDesc);
//                    break;
//                case WifiManager.NETWORK_STATE_CHANGED_ACTION:
//                    //int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
//                    break;
//                case ConnectivityManager.CONNECTIVITY_ACTION:
//                    //QdLog.println("网络状态变化,hash:" + hashCode() + ",action=" + action + "," + des);
//                    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//                    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//                    if (networkInfo != null) { //如果无网络连接networkInfo为null
//                       // dispatchConnectedEvent(context, intent);
//                    } else { //网络连接
//                       // dispatchDisConnectedEvent(context, intent);
//                    }
//                    break;
//            }
//        }


        //获取手机的连接服务管理器，这里是连接管理器类
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo wifiNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobileNetworkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo activeNetworkInfo = cm.getActiveNetworkInfo();

        NetworkInfo.State wifiState = NetworkInfo.State.UNKNOWN;
        NetworkInfo.State mobileState = NetworkInfo.State.UNKNOWN;

        Bundle bundle = intent.getExtras();
        NetworkInfo netInfo = null;
        if(bundle != null){
            netInfo = (NetworkInfo) bundle.get(ConnectivityManager.EXTRA_NETWORK_INFO);
        }

        if (wifiNetworkInfo != null) {
            wifiState = wifiNetworkInfo.getState();
        }
        if (mobileNetworkInfo != null) {
            mobileState = mobileNetworkInfo.getState();
        }

        if (activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting()) {
            onConnected(isReconnect,context,intent);
                isReconnect = false;
        } else if (activeNetworkInfo == null){
                isReconnect = true;
                onUnConnected();
        }

        if (NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED == mobileState) {
            Log.d(TAG, "onWifiTo4G()");
            onWifiTo4G();
        } else if (NetworkInfo.State.CONNECTED == wifiState && NetworkInfo.State.CONNECTED != mobileState
                && netInfo != null && netInfo.getType() == 1) {
                /*
                    4G 切换 wifi 时候，首先会收到 4G 断开的通知，再收到 wifi 连接的通知，会导致
                    该回调被调用两次。netInfo.getType()：1：wifi，0：4G
                 */
            on4GToWifi();
        } else if (NetworkInfo.State.CONNECTED != wifiState && NetworkInfo.State.CONNECTED != mobileState) {
            onDisConnected(context,intent);
        }
    }
    @Override
    public void onUnConnected() {

    }

    @Override
    public void on4GToWifi() {

    }

    @Override
    public void onWifiTo4G() {

    }
    @Override
    public void onConnected(boolean isReconnect, Context context, Intent intent) {

    }

    @Override
    public void onDisConnected(Context context, Intent intent) {

    }

//    public void dispatchConnectedEvent(Context context, Intent intent) {
//        if (netState != 1) {
//            netState = 1;
//            String des = "";
//            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo networkInfo = manager.getActiveNetworkInfo();
//            if (networkInfo != null) { //如果无网络连接activeInfo为null
//                des = NetWatchdog.getNetworkTypeDescription(networkInfo.getType());
//                QdLog.println("网络[连接]:" + des);
//                onConnected(context,intent);
//            }
//        }
//    }
//
//    public void dispatchDisConnectedEvent(Context context, Intent intent) {
//        if (netState != 0) {
//            netState = 0;
//            String des = NetWatchdog.getActionDescription(intent.getAction());
//            QdLog.println("网络[断开]:" + des);
//            onDisConnected(context,intent);
//        }
//    }


}
